<template>
  <div class="commentList">
    <div style="background: #ffffff;   padding-left: 10px;">
      <div class="commentList-title">
        评论
        <rt-attr
          :text="comments == 0 ? '' : comments"
          style="display: inline-block;"
          text-size="small"
        ></rt-attr>
      </div>
      <div class="line"></div>
      <div>
        <comment-item
          v-for="(item, index) in commentDatas"
          :key="index"
          :tenant-code="tenantCode"
          :object_id="object_id"
          :object_name="object_name"
          :object_type_name="object_type_name"
          :comment-data="item"
          @on-action-changed="onActionChanged"
        >
          <div v-if="item.childrenList && item.childrenList.length > 0">
            <comment-item
              v-for="(element, index) in item.childrenList"
              :key="index"
              :tenant-code="tenantCode"
              :object_id="object_id"
              :object_name="object_name"
              :object_type_name="object_type_name"
              :comment-data="element"
              type="second"
              @on-action-changed="onActionChanged"
            ></comment-item>
          </div>
        </comment-item>
      </div>
    </div>
    <div class="noContent">
      ————没有更多了————
    </div>
  </div>
</template>

<script>
import commentItem from './commentItem';

export default {
  name: 'RtCommentList',
  components: {
    commentItem
  },
  props: {
    object_id: {
      type: String,
      required: true
    },
    object_name: {
      type: String,
      required: true
    },
    object_type_name: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      commentDatas: [],
      comments: 0,
      tenantCode: rt.getTenantCode()
    };
  },
  async created() {
    const vm = this;
    const object_id = this.object_id;
    const tenantCode = this.tenantCode;
    vm.loadComments(object_id, tenantCode);
    this.$bus.$on('reload-commentDatas', () => {
      vm.loadComments(object_id, tenantCode);
    });
  },
  methods: {
    // 用来隐藏IOS actionsheet foot显示
    onActionChanged(val) {
      this.$emit('on-action-changed', val);
    },
    loadComments(object_id, tenantCode) {
      rt.dmGet(
        `api/DmPostComment/GetCommentListById?objectId=${object_id}&tenantCode=${tenantCode}`
      ).then(resp => {
        this.commentDatas = resp;
        this.comments = 0;
        if (this.commentDatas && this.commentDatas.length > 0) {
          this.comments = this.commentDatas.length;
          this.commentDatas.forEach(item => {
            if (item.childrenList && item.childrenList.length > 0) {
              this.comments += item.childrenList.length;
            }
          });
        }
        this.$emit('on-commentcount-change', this.comments);
      });
    }
  }
};
</script>

<style lang="less" scoped>
.commentList {
  margin-top: 10px;
  .line {
    background: #f0f0f0;
    height: 1px;
    margin-right: 15px;
  }
  .commentList-title {
    padding: 5px 0;
    font-size: 15px;
    color: #222222;
    font-weight: 500;
  }
}
.noContent {
  text-align: center;
  font-size: 12px;
  color: #cccccc;
  padding: 8px 0 10px 0;
}
.footer {
  background: #ffffff;
  display: flex;
  align-items: center;
  .footer-comment {
    flex: 7;
    margin: 6px 10px;
    height: 32px;
    line-height: 32px;
    background: rgba(0, 0, 0, 0.1);
    color: #cccccc;
    border-radius: 16px;
    padding-left: 15px;
  }
  .footer-like {
    flex: 1;
    font-size: 14px;
  }
  .footer-dislike {
    color: #888888;
  }
  .footer-liked {
    color: #53a4fc;
  }
}
</style>
